//
//  IPlayerController.swift
//  iPlayer
//
//  Created by jackcheng on 2017/11/10.
//  Copyright © 2017年 xiangyue. All rights reserved.
//

import UIKit
import IJKMediaFrameworkWithSSL

class IPlayerController: IPViewController {

	
	var player:IJKFFMoviePlayerController!
	var urls:[String]!
	
	override var supportedInterfaceOrientations: UIInterfaceOrientationMask  {
		return .landscape
	}
	
	override var shouldAutorotate: Bool {
		return true
	}
	
	init(urls:[String]) {
		super.init(nibName: nil, bundle: nil)
		self.urls = urls
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
    override func viewDidLoad() {
        super.viewDidLoad()

		self.reloadUrl(urlString: self.urls.first!)
		
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	func shutDown() {
		if (self.player != nil) {
			self.player.shutdown()
			self.player.view.removeFromSuperview()
			self.player = nil
		}
	}
	
	func reloadUrl(urlString:String) {
		
		if self.player != nil {
			self.player.shutdown()
		    self.player.view.removeFromSuperview()
			self.player = nil
		}
		
		IJKFFMoviePlayerController.checkIfFFmpegVersionMatch(true)
		let options = IJKFFOptions.byDefault()
		self.player = IJKFFMoviePlayerController.init(contentURLString: urlString, with: options)
		self.player.setPauseInBackground(true)
		self.player.scalingMode = .aspectFit
		self.player.shouldAutoplay = true
		self.view.insertSubview(self.player.view, at: 0)
		self.player.view.mas_makeConstraints { (make) in
			make?.top.mas_equalTo()(0)
			make?.bottom.mas_equalTo()(0)
			make?.left.mas_equalTo()(0)
			make?.right.mas_equalTo()(0)
		}
		self.player.prepareToPlay()
	}
	
	func installNotificaions() {
		NotificationCenter.default.addObserver(self, selector: #selector(loadStateDidChange(fication:)), name: NSNotification.Name.IJKMPMoviePlayerLoadStateDidChange, object: nil)
		NotificationCenter.default.addObserver(self, selector: #selector(moviePlayBackDidFinish(fication:)), name: NSNotification.Name.IJKMPMoviePlayerPlaybackDidFinish, object: nil)
		NotificationCenter.default.addObserver(self, selector: #selector(mediaIsPreparedToPlayDidChange(fication:)), name: NSNotification.Name.IJKMPMediaPlaybackIsPreparedToPlayDidChange, object: nil)
		NotificationCenter.default.addObserver(self, selector: #selector(moviePlayBackStateDidChange(fication:)), name: NSNotification.Name.IJKMPMoviePlayerPlaybackStateDidChange, object: nil)
	}
	
	@objc func loadStateDidChange(fication:Notification) {
		
	}
	
	@objc func moviePlayBackDidFinish(fication:Notification) {
		
	}
	
	@objc func mediaIsPreparedToPlayDidChange(fication:Notification) {
		
	}
	
	@objc func moviePlayBackStateDidChange(fication:Notification) {
		
	}
	
	func removeNotifications() {
		NotificationCenter.default.removeObserver(self)
	}
    
	@IBAction func exit(_ sender: UIButton) {
		self.shutDown()
		self.removeNotifications()
		self.dismiss(animated: true, completion: nil)
	}
	
	
	
	
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
